package com.yinbo.model;

/**
 * Page object is used to store and fetch page information.
 * 
 * @author Y.N
 */
public class Page {

	public static final int DEFAULT_PAGE_SIZE = 10;

	private int pageIndex;
	private int pageSize;
	private int totalCount;
	private int pageCount;

	/**
	 * Specify page index (starts from 1) and page size.
	 * 
	 * @param pageIndex
	 *            Starts from 1.
	 * @param pageSize
	 *            How many items a page can display.
	 */
	public Page(int pageIndex, int pageSize) {
		// check:
		if (pageIndex < 1 || pageSize < 1 || pageSize > 100)
			throw new IllegalArgumentException();
		this.pageIndex = pageIndex;
		this.pageSize = pageSize;
	}

	/**
	 * Specify page index (starts from 1) and default page size (20).
	 * 
	 * @param pageIndex
	 *            Starts from 1.
	 */
	public Page(int pageIndex) {
		this(pageIndex, DEFAULT_PAGE_SIZE);
	}

	/**
	 * Get page index.
	 */
	public int getPageIndex() {
		return pageIndex;
	}

	/**
	 * Get page size.
	 */
	public int getPageSize() {
		return pageSize;
	}

	/**
	 * Get how many pages.
	 */
	public int getPageCount() {
		return pageCount;
	}

	/**
	 * Get how many items in total.
	 */
	public int getTotalCount() {
		return totalCount;
	}

	/**
	 * Get index of the first item in this page.
	 */
	public int getFirstResult() {
		return (pageIndex - 1) * pageSize;
	}

	/**
	 * Get index of the last item in this page.
	 */
	public int getLastResult() {
		if (totalCount == 0)
			return 0;
		int last = (pageIndex) * pageSize - 1;
		return last >= totalCount ? totalCount - 1 : last;
	}

	/**
	 * Has previous page?
	 */
	public boolean getHasPrevious() {
		return pageIndex > 1;
	}

	/**
	 * Has next page?
	 */
	public boolean getHasNext() {
		return pageIndex < pageCount;
	}

	/**
	 * Set a total count of items.
	 */
	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
		pageCount = totalCount / pageSize
				+ (totalCount % pageSize == 0 ? 0 : 1);
		// adjust pageIndex:
		if (totalCount == 0) {
			if (pageIndex != 1)
				throw new IndexOutOfBoundsException("Page index out of range.");
		} else {
			if (pageIndex > pageCount)
				throw new IndexOutOfBoundsException("Page index out of range.");
		}
	}

	/**
	 * Is there any items? It is equal of getTotalCount()==0.
	 */
	public boolean isEmpty() {
		return totalCount == 0;
	}

	public String getPagination(String pageUrl) {
		if (this.getPageCount() < 1) {
			return "";
		}
		StringBuffer sb = new StringBuffer("");

		sb.append(" <ul id=\"pagination-digg\">  ");
		if(pageUrl.indexOf("?")<0){
			pageUrl+="?1=1";
		}
		if (this.getPageCount() > 7) {
			if (this.getPageIndex() == 1) {
				//sb.append(" <li class=\"previous-off\">&laquo;上一页</li>  ");
			} else {
				sb.append("<li class=\"previous\"><a href=\"" + pageUrl
						+ "&pageNo=" + (this.getPageIndex() - 1)
						+ "\">&laquo;上一页</a></li>  ");
			}
			
			int startView = pageIndex-3;
			if(startView<1){
				startView = 1;
			}
			
			for (int i = startView; i <= pageIndex+4 && i<=pageCount; i++) {
				if (i == this.getPageIndex()) {
					sb.append(" <li class=\"active\" >" + i + "</li>  ");
				} else {
					sb.append(" <li><a href=\"" + pageUrl + "&pageNo=" + i
							+ "\">" + i + "</a></li>  ");
				}
			}
			
			if (this.getPageIndex() < this.getPageCount()) {
				sb.append(" <li class=\"next\"><a href=\"" + pageUrl
						+ "&pageNo=" + (this.getPageIndex() + 1)
						+ "\">下一页 &raquo;</a></li>  ");
			} else {
				//sb.append(" <li class=\"next-off\">下一页 &raquo;</li>  ");
			}
			
		} else {
			for (int i = 1; i <= this.getPageCount(); i++) {
				if (i == this.getPageIndex()) {
					sb.append(" <li  class=\"active\" >" + i + "</li>  ");
				} else {
					sb.append(" <li><a href=\"" + pageUrl + "&pageNo=" + i
							+ "\">" + i + "</a></li>  ");
				}
			}
		}

		sb.append("</ul>  ");
		return sb.toString();
	}

}
